package com.amikomgamedev.labirinhantu.state;


import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;

import android.content.Intent;
import android.net.Uri;
import android.view.KeyEvent;

import com.amikomgamedev.labirinhantu.grafik.Grafik_Define;

public class State_Menu_Main	extends		State_Menu_Help
								implements	Grafik_Define
{
	private int moveLevel	= 0;	
	private int moveCredit	= 1;
	private int moveHelp	= 2;
	private int moveSelectStage = 3;
	private int	moveCamera	= -1;
	
	protected void create_Menu_Main()
	{
//		m_Ads.visibleAds();
		
		moveCamera = -1;
		
		//Sound_Manager.playMenuBgm();
		sound.playMenuBgm();
//		sound.stopBgmStage1();
		m_Camera_Menu.initCamera(grafik.spr_Menu_Main_Bg);
	}
	
	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent)
	{
		if (State_Menu_Current == STATE_MENU_MAIN)
		{
			m_ClickDetector.onTouchEvent(pSceneTouchEvent);
		}
		
		return super.onSceneTouchEvent(pScene, pSceneTouchEvent);
	}
	
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event)
	{
		if (State_Menu_Current == STATE_MENU_MAIN)
		{
			if (keyCode == KeyEvent.KEYCODE_BACK)
				finish();
		}
		
		return super.onKeyUp(keyCode, event);
	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX,
			float pTouchAreaLocalY)
	{
		if(State_Menu_Current == STATE_MENU_MAIN)
		{
			if(pSceneTouchEvent.isActionUp())
			if(pTouchArea.equals(grafik.spr_Menu_Main_Btn[MENU_MAIN_BTN_PLAY]))
			{
				grafik.lst_Menu_Stage_Lingkaran.get(0).setCurrentTileIndex(0);
				grafik.lst_Menu_Stage_Lingkaran.get(1).setCurrentTileIndex(1);
				grafik.lst_Menu_Stage_Lingkaran.get(2).setCurrentTileIndex(1);
				
				//m_Camera_Menu.initCamera(grafik.spr_Menu_Level_Bg);
				//moveCamera	= moveLevel;
				m_Camera_Menu.initCamera(grafik.spr_Menu_Select_Stage[0]);
				moveCamera = moveSelectStage;
				
			}
			else if(pTouchArea.equals(grafik.spr_Menu_Main_Btn[MENU_MAIN_BTN_CREDITS]))
			{

				m_Camera_Menu.initCamera(grafik.spr_Menu_Credits_Bg);				
				moveCamera	= moveCredit;					

			}
			else if(pTouchArea.equals(grafik.spr_Menu_Main_Btn[MENU_MAIN_BTN_HELP]))
			{
			    
				grafik.lst_Menu_Help_Lingkaran.get(0).setCurrentTileIndex(1);
				grafik.lst_Menu_Help_Lingkaran.get(1).setCurrentTileIndex(0);
				
				m_Camera_Menu.initCamera(grafik.spr_Menu_Help[0]);				
				moveCamera	= moveHelp;
			
			}
			else if (pTouchArea.equals(grafik.spr_Icon_Social[0]))
			{
			    	startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/cupcorngames")));
			}
			else if (pTouchArea.equals(grafik.spr_Icon_Social[1]))
			{
			    	startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://twitter.com/amikomgamedev")));
			}
		}
		return super.onAreaTouched(pSceneTouchEvent, pTouchArea, pTouchAreaLocalX,
				pTouchAreaLocalY);
	}
	
	@Override
	public void onUpdate(float pSecondsElapsed)
	{
		if(State_Menu_Current == STATE_MENU_MAIN)
		{
			
			if (moveCamera == moveLevel)
			{
				m_Camera_Menu.moveCamera(pSecondsElapsed);
				
				if (m_Camera_Menu.isStopCamera())
					switchState(STATE_MENU_LEVEL);
			}
			else if (moveCamera == moveHelp)
			{
				m_Camera_Menu.moveCamera(pSecondsElapsed);
				
				if (m_Camera_Menu.isStopCamera())
					switchState(STATE_MENU_HELP);
			}
			else if (moveCamera == moveCredit)
			{
				m_Camera_Menu.moveCamera(pSecondsElapsed);
				
				if (m_Camera_Menu.isStopCamera())
					switchState(STATE_MENU_CREDITS);
			}
			else if(moveCamera == moveSelectStage)
			{
				m_Camera_Menu.moveCamera(pSecondsElapsed);
				
				if(m_Camera_Menu.isStopCamera())
					switchState(STATE_MENU_SELECT_STAGE);
			}
		}
				
		
		super.onUpdate(pSecondsElapsed);
	}
	
	@Override
	public synchronized void onPauseGame()
	{
	    if(State_Menu_Current == STATE_MENU_MAIN)
		sound.stopMenuBgm();
	    
	    super.onPauseGame();
	}
	
	@Override
	public synchronized void onResumeGame()
	{
	    if(State_Menu_Current == STATE_MENU_MAIN)
		sound.playMenuBgm();

	    super.onResumeGame();
	}
}
